package com.bzhang.springcloud.controller;

import com.bzhang.springcloud.service.PaymentService;
import com.bzhang.springcloud.service.imp.PaymentFallbackService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ：Bin.Z
 * @description：
 * @date ：2022/6/29 14:16
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentGlobalTimeOutFallbackMethod")
public class OrderHystrixController {

    @Resource
    private PaymentService paymentService;
    @Resource
    private PaymentFallbackService paymentCircuitBreaker;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id) {
        String result = paymentService.paymentInfo_OK(id);
        return result;
    }

//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",
//        commandProperties = {
//                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
//        })
//    @HystrixCommand
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String result = paymentService.paymentInfo_TimeOut(id);
        return result;
//        int age = 10/0;
//        String result = paymentService.paymentInfo_TimeOut(id);
//        return result;
    }
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
        return "我是消费者81,id对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }
    public String paymentGlobalTimeOutFallbackMethod() {
        return "Global 消费者81,异常";
    }

    //===服务熔断
    @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        String result = paymentCircuitBreaker.paymentCircuitBreaker(id);
        log.info("*******result:"+result);
        return result;
    }
}
